--
-- Author: shunguo.chen
-- Date: 2019-05-17 11:22:58
--
-- LegendSkillRender 传奇技能
--

local ClassRef = app.mvc.createListViewCell(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.LegendSkillNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    self:setContentSize(self._ui.Panel:getContentSize())

    self._ui.selected_btn:addClickEventListener(function()
        if self._data and self._cellModel then
            self:dispatchCellEvent("select", self)
        end
    end)

    self._ui.btn_add:addClickEventListener(function()
        if self._data and self._cellModel then
            self:dispatchCellEvent("open", self)
        end
    end)

    -- 红点
    self._dotNode = self._ui.btn_add:getChildByName("dot")

    local iconNode = self._ui.icon
    self._iconChooseBg = iconNode:findChild("Panel/bg_choose")
    self._icon = iconNode:findChild("Panel/icon")
    self._costNumBg = iconNode:findChild("Panel/icon_num")
    self._costNumText = iconNode:findChild("Panel/num")

    self:refresh()
end

-- data = Skill
function ClassRef:refresh()
    if self._data then
        local skill = self._data
        local isOpen = skill:isLegendOpen()
        local isChoose = skill:isLegendChoose()

        self._ui.name:setString(skill:getName())
        self._ui.lv:setString(string.format(L("duelGametips9"), skill:getSkillLv()))

        local tipStr
        if isOpen then
            tipStr = isChoose and L("LegendBasic3_ui6") or L("LegendBasic3_ui5")
        else
            tipStr = L("unactivate")
        end
        self._ui.tips:setString(tipStr)

        self._ui.bg_1:setVisible(not isOpen)
        self._ui.selected_btn:setVisible(isOpen)
        self._ui.btn_add:setVisible(not isOpen)
        self:setSelected(isChoose)

        -- 红点
        self._dotNode:setVisible(skill:isLegendCanOpen())

        -- icon
        self._icon:loadTexture(skill:getIcon())
        local costNum, costPath = skill:getCostByClass()
        local costColor = skill:getCostColorByClass()
        self:setManaCost(costNum, costColor)
    end
end

function ClassRef:setManaCost(num, color)
    self._costNumBg:setVisible(num > 0)
    self._costNumText:setVisible(num > 0)
    if num > 0 then
        self._costNumText:setString(num)
        self._costNumText:setTextColor(color or display.COLOR_WHITE)
    end
end

function ClassRef:isSelected()
    return self._iconChooseBg:isVisible()
end

function ClassRef:setSelected(b)
    self._iconChooseBg:setVisible(b)
    self._ui.selected_btn:setBright(not b)
end

function ClassRef:getActiveBtn()
    return self._ui.btn_add
end

return ClassRef
